/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/5/20 20:20
 */
// 二维dp
    // dp[i][j]:标识区间[i,j]是否为会回文子串
class Solution647 {
    public int countSubstrings(String s) {
        char[] chars = s.toCharArray();
        boolean[][] dp = new boolean[chars.length][chars.length];
        int len = chars.length;
        int result = 0;
        for (int i = len; i >= 0; i--) {
            for (int j = i; j < len; j++) {
                if (chars[i] == chars[j]) {
                    if (j - i <= 1) {
                        result++;
                        dp[i][j] = true;
                    } else if (dp[i + 1][j - 1]) {
                        result++;
                        dp[i][j] = true;
                    }
                }
            }
        }
        return result;
    }
}
